package edu.sctu.practice.class01.zhoumengxue.day1126;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;

/**
 * http://www.bxwx.org/b/70/70093/index.html
 * Created by ASUS on 2015/11/26.
 * <p>
 * <p>
 * <a href="" data = ""></a>   // href和data就是attribute
 * <p>
 * <div id="demo"></div>
 * $(#demo)
 * <p>
 * <li class="aa"></li>
 * $(.aa)
 */
public class Client {
    public static void main(String[] args) {

        String url = "http://www.bxwx.org/b/70/70093/index.html";
        try {
            //DOM树
            Document document = Jsoup.connect(url).timeout(5000).get();

            Elements elements = document.select("dd > a");


            for (Element element : elements) {

                String title = element.text();
                String detailURL = element.attr("href");

 //               System.out.println(element.text());
 //               System.out.println(element.attr("href"));

                Files.append((element.text() + "---" + element.attr("href")), new File("F:\\小说\\小说.txt"), Charsets.UTF_8);
                Files.append("\r\n", new File("F:\\小说\\小说.txt"), Charsets.UTF_8);

                getContent(title, detailURL);
            }



            //System.out.println(document);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void getContent(String title, String detailURL) {

        try {
            Document document = Jsoup.connect("http://www.bxwx.org/b/70/70093/"+ detailURL).timeout(3000).get();

            Element element = document.select("div#content").get(0);
            String content = element.text();

            Files.createParentDirs(new File("F:\\小说\\完美世界\\demo.txt"));

            Files.write(content.getBytes(), new File("F:\\小说\\完美世界\\" + title + ".txt"));


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

